package com.yxw.live_vod_boot.utils;


import com.yxw.live_vod_boot.exception.GlobalCustomerException;
import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class AssertUtil {

    //手机的正则表达式
    private static final Pattern CHINA_PATTERN_PHONE = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");


    /**--------------------------------------------------------
     手机号断言
     --------------------------------------------------------**/
    public static void isPhone(String phone){
        //1.非空校验
        isNotEmpty(phone);
        //2.格式校验
        Matcher m = CHINA_PATTERN_PHONE.matcher(phone);
        if(!m.matches()){
            throw new GlobalCustomerException(ErrorCodeEnum.COMM_SMS_PHONE_ERROR);
        }
    }


    /**--------------------------------------------------------
     断言 不为空，如果为空，抛异常
     --------------------------------------------------------**/
    public static void isNotEmpty(String text) {
        if (text == null || text.trim().isEmpty()) {
            throw new GlobalCustomerException(ErrorCodeEnum.STR_EMPTY);
        }
    }

    public static void isNotEmpty(byte[] body) {
        if (body == null || body.length == 0) {
            throw new GlobalCustomerException(ErrorCodeEnum.ARR_EMPTY);
        }
    }

    public static <T>void isNotEmpty(List<T> body) {
        if (body == null || body.isEmpty()) {
            throw new GlobalCustomerException(ErrorCodeEnum.ARR_EMPTY);
        }
    }


    /**--------------------------------------------------------
     断言对象为空
     --------------------------------------------------------**/
    public static void isNull(Object obj , ErrorCodeEnum errorCodeEnum){
        if(obj != null){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }
    public static void isNotNull(Object obj , ErrorCodeEnum errorCodeEnum){
        if(obj == null){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }

    /**--------------------------------------------------------
     断言false,如果为true,我报错
     --------------------------------------------------------**/
    public static void isFalse(boolean isFalse , ErrorCodeEnum errorCodeEnum){
        if(isFalse){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }
    public static void isTrue(boolean isTrue , ErrorCodeEnum errorCodeEnum){
        if(!isTrue){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }


    /**--------------------------------------------------------
     断言两个字符串一致
     --------------------------------------------------------**/
    public static void isEquals(String s1,String s2 , ErrorCodeEnum errorCodeEnum){
        isNotEmpty(s1);
        isNotEmpty(s2);
        if(!s1.equals(s2)){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }
    public static void isEqualsTrim(String s1,String s2 , ErrorCodeEnum errorCodeEnum){
        isNotEmpty(s1);
        isNotEmpty(s2);
        if(!s1.trim().equals(s2.trim())){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }

    public static void isEqualsIgnoreCase(String s1,String s2 , ErrorCodeEnum errorCodeEnum){
        isNotEmpty(s1);
        isNotEmpty(s2);
        if(!s1.trim().equalsIgnoreCase(s2.trim())){
            throw new GlobalCustomerException(errorCodeEnum);
        }
    }

}